#include <xscript/xscriptexecutor.h>
#include <xscript/xprint.h>
#include <astree/xabstractsyntaxtreebuilder.h>

std::shared_ptr<XScriptExecutor> XScriptExecutor::executor = std::make_shared<XScriptExecutor>();

std::shared_ptr<XScriptExecutor> XScriptExecutor::getInstance() {
    return executor;
}

XScriptExecutor::XScriptExecutor() {
    treeBuilder = std::make_shared<XAbstractSyntaxTreeBuilder>();
    variableTable = std::make_shared<XVariableTable>();
}

void XScriptExecutor::setTokens(const XTokenBlock& inputToken) {
    tokenBlock = inputToken;
}

bool XScriptExecutor::execute() {
    if (tokenBlock.empty())
        return true;

    auto tree = treeBuilder->build(tokenBlock);
    if (tree) {
        return tree->invoke(variableTable);
    }
    return false;
}

std::shared_ptr<XVariableTable> XScriptExecutor::getVariableTable() {
    return variableTable;
}
